<!--  首页的商品list-->
<template>
  <div class="index-list" v-infinite-scroll="loadMore" infinite-scroll-distance="10">
    <!-- tab切换栏 -->
    <div class="tabs">
      <ul>
        <li v-for="(tabData, index) in tabDatas" :key="tabData.id" @click="tabChange(index, tabData.type)">
          <h4>{{ tabData.title }}</h4>
          <span :class="activeIndex === index ? 'active' : ''">{{
              tabData.tip
          }}</span>
        </li>
      </ul>
    </div>
    <div class="goods-list" v-if="goodsListDatas">
      <ul>
        <li v-for="goodsListData in goodsListDatas" :key="goodsListData.goods_id"
          @click="toGoodsDetail(goodsListData.goods_id)">
          <!-- 商品的图片 -->
          <div class="goods-img">
            <img :src="goodsListData.goods_thumb" alt="" />
          </div>
          <!-- 商品题目及价格label -->
          <div class="info-box">
            <div class="title">{{ goodsListData.title }}</div>
            <!-- 现价 -->
            <div class="currency-price">
              <em>¥</em>
              <!-- 整数 -->
              <span>{{ goodsListData.shop_price | setInt }}</span>
              <!-- 小数 -->
              <em>{{ goodsListData.shop_price | setFloat }}</em>
            </div>
            <div class="label-list" v-if="goodsListData.goods_label.length > 0">
              <a href="" v-for="label in goodsListData.goods_label" :key="label.label_id">
                <img :src="label.formated_label_image" alt="" /></a>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <!-- 商品列表 -->
    <div class="loading" v-if="showloadingFlag">
      <img src="@/assets/images/loading.gif" alt="" />
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { HomeListApi } from "@/api/home.js";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      tabDatas: [
        { id: "1", title: "精选", tip: "为你推荐", type: "is_best" },
        { id: "2", title: "社区", tip: "新奇好物", type: "is_new" },
        { id: "3", title: "新品", tip: "潮流上新", type: "is_new" },
        { id: "4", title: "热卖", tip: "火热爆款", type: "is_hot" },
      ],
      goodsListDatas: null,
      // goodsListDatas: [
      //   {
      //     goods_id: 672,
      //     shop_price: "2333.00",
      //     shop_price_formated: "¥2333.00",
      //     market_price: "2799.60",
      //     market_price_formated: "¥2799.60",
      //     title:
      //       "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
      //     goods_thumb:
      //       "https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1489108266095.jpg",
      //     goods_label: [],
      //   },
      //   {
      //     goods_id: 1170,
      //     shop_price: "269.00",
      //     shop_price_formated: "¥269.00",
      //     market_price: "398.00",
      //     market_price_formated: "¥398.00",
      //     goods_number: 499,
      //     title:
      //       "花花公子（PLAYBOY）商务男士西服套装男修身职业装正装上班工作西装结婚新郎礼服外套 黑色两粒扣（",
      //     goods_thumb:
      //       "https://img14.360buyimg.com/n1/s300x300_jfs/t1/114949/5/9043/122341/5ed61b3cEcd804cf6/b8f8390eb7828099.jpg",
      //     goods_label: [
      //       {
      //         label_id: "1",
      //         label_name: "双十二",
      //         formated_label_image:
      //           "https://x.dscmall.cn/storage/data/goods/label/双十二2.png",
      //       },
      //       {
      //         label_id: "2",
      //         label_name: "商创精选",
      //         formated_label_image:
      //           "https://x.dscmall.cn/storage/data/goods/label/商创精选2.png",
      //       },
      //       {
      //         label_id: "3",
      //         label_name: "放心购",
      //         formated_label_image:
      //           "https://x.dscmall.cn/storage/data/goods/label/放心购2.png",
      //       },
      //     ],
      //   },
      //   {
      //     goods_id: 1163,
      //     shop_price: "488.00",
      //     shop_price_formated: "¥488.00",
      //     market_price: "988.00",
      //     market_price_formated: "¥988.00",
      //     title:
      //       "VRSZEE轻奢品牌女装风衣外套女中长款2020秋季新款韩版流行小个子气质修身双排扣 卡其色 M",
      //     goods_thumb:
      //       "https://x.dscmall.cn/storage/images/202109/thumb_img/1163_thumb_G_1631915644325.jpg",
      //     goods_label: [],
      //   },
      // ],
      activeIndex: 0,
      showloadingFlag: true,
      page: 1,
      size: 10,
      type: "is_best",
    };
  },
  //计算属性 类似于data概念
  computed: {},
  props: ['showFlag'],
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    tabChange(index, type) {
      this.activeIndex = index;
      // 显示loading
      this.showloadingFlag = true;
      this.goodsListDatas = null;
      this.type = type;
      // 还原到第一页
      this.page = 1;
      // 请求初始数据
      this.getListDatas();
    },
    toGoodsDetail(goods_id) {
      this.$router.push("/goodsdetail/" + goods_id);
    },
    // 初始数据
    getListDatas() {
      HomeListApi({
        page: this.page,
        size: this.size,
        type: this.type,
      }).then((res) => {
        console.log(res.data.data);
        this.goodsListDatas = res.data.data;
        this.showloadingFlag = false;
      });
    },
    // 加载更多
    loadMore() {
      // this.showFlag:现在组件处理HomeIndex首页的首页
      console.log(this.showFlag);
      if (this.showFlag) {
        this.showloadingFlag = true;
        this.page++;
        console.log("到底了");
        // 多次请求新的数据
        this.getmoreDatas();
      }
    },
    // 加载跟多的数据请求
    getmoreDatas() {
      HomeListApi({
        page: this.page,
        size: this.size,
        type: this.type,
      }).then((res) => {
        console.log(res.data.data);
        this.goodsListDatas =
          this.goodsListDatas && this.goodsListDatas.concat(res.data.data);
        // loading隐藏
        this.showloadingFlag = false;
      });
    },
  },
  beforeCreate() { }, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() { },
  beforeMount() { }, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // setTimeout(() => {
    //   this.showloadingFlag = false;
    // }, 1000);
    this.getListDatas();
  },
  beforeUpdate() { }, //生命周期 - 更新之前
  updated() { }, //生命周期 - 更新之后
  beforeDestroy() { }, //生命周期 - 销毁之前
  destroyed() { }, //生命周期 - 销毁完成
  activated() { }, //如果页面有keep-alive缓存功能，激活
  deactivated() { }, //如果页面有keep-alive缓存功能，非激活
};
</script>
<style lang="less">
.index-list {
  width: calc(100% - 2rem);
  margin: 0 auto;
  min-height: 750px;

  // tab栏
  .tabs {
    ul {
      height: 4rem;
      padding: 1rem 0;
      display: flex;
      justify-content: center;
      align-items: center;

      li {
        flex: 1;
        text-align: center;
        // 子绝父相
        position: relative;

        &::after {
          content: " ";
          position: absolute;
          height: 80%;
          width: 1px;
          right: 0;
          top: 10%;
          background-color: #ccc;
        }

        // 伪类
        &:last-child::after {
          content: "";
          height: 0;
        }

        h4 {
          font-size: 1.6rem;
        }

        span {
          font-size: 1.2rem;
          color: #888;
          border-radius: 1rem;
          padding: 0.1rem 0.8rem;
          margin-top: 0.2rem;
        }

        .active {
          background: linear-gradient(-88deg, #ff4f2e, #f91f28);
          color: #fff;
        }
      }
    }
  }

  // 商品列表样式
  .goods-list {
    ul {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;

      li {
        width: 17.2rem;
        border-radius: 1rem;
        margin: 1rem 0;
        background-color: #fff;
        overflow: hidden;

        .goods-img {
          width: 17.2rem;
          height: 17.2rem;

          img {
            width: 100%;
            height: 100%;
          }
        }

        .info-box {
          padding: 1rem;

          // 题目
          .title {
            font-size: 1.5rem;
            color: #000;
            // 多行文本省略号
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
          }

          // 现价
          .currency-price {
            margin: 0.5rem 0;
            font-weight: 700;
            color: rgb(228, 49, 36);
            font-size: 1.8rem;

            em {
              font-size: 1.2rem;
            }
          }

          // 标签
          .label-list {
            display: flex;

            a {
              height: 1.6rem;
              margin: 0 0.5rem 0 0;

              img {
                height: 100%;
                width: auto;
              }
            }
          }
        }
      }
    }
  }

}

.loading {
  height: 10rem;
  text-align: center;
  margin: rem auto;

  img {
    width: 3rem;
    opacity: 0.5;
  }
}
</style>